package com.haralabidis.jdeveloper.extension.subversionmonitor.util.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import oracle.xml.jaxp.JXDocumentBuilder;
import oracle.xml.jaxp.JXDocumentBuilderFactory;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.v2.XMLElement;


public class XMLBuilder {
    private XMLDocument xml_document = null;
    private XMLElement root_element = null;

    protected static class Constants {
        public static class XMLTags {
            public static String LOCATION_TAG = "Location";
        }

        public static class XMLData {
            public static String VERSION_DATA = "1.0";
            public static String ENCODING_DATA = "UTF-8";
        }
    }
    
    public XMLBuilder(String root_element_name, String attrib_name, String attrib_data) {
        JXDocumentBuilderFactory factory =
            (JXDocumentBuilderFactory)JXDocumentBuilderFactory.newInstance();
        JXDocumentBuilder documentBuilder;
        try {
            documentBuilder = (JXDocumentBuilder)factory.newDocumentBuilder();

            xml_document = (XMLDocument)documentBuilder.newDocument();
            xml_document.setVersion(XMLBuilder.Constants.XMLData.VERSION_DATA);
            xml_document.setEncoding(XMLBuilder.Constants.XMLData.ENCODING_DATA);
            
            root_element =
                    (XMLElement)xml_document.createElement(root_element_name);

            root_element.setAttribute(attrib_name, attrib_data);
            xml_document.appendChild(root_element);
        } catch (ParserConfigurationException e) {
        }
    }

    public XMLDocument getDocument() {
        return this.xml_document;
    }

    public XMLElement getRootElement() {
        return this.root_element;
    }

    public void writeXMLToFile(String filename) {
        OutputStream xml_output;
        try {
          TransformerFactory transformerFactory = TransformerFactory.newInstance();
          Transformer transformer = transformerFactory.newTransformer();  
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          DOMSource source = new DOMSource(xml_document);
            
          xml_output = new FileOutputStream(new File(filename));
          StreamResult result = new StreamResult(xml_output);
          transformer.transform(source, result);
          xml_output.flush();
          xml_output.close();
        } catch (TransformerConfigurationException e) {
        } catch (TransformerException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

    public XMLElement addFileElement(XMLElement parent_element,
                                     String element_name,
                                     String attribute_name) {
        XMLElement child_element =
            (XMLElement)xml_document.createElement(element_name);
        child_element.setAttribute(XMLBuilder.Constants.XMLTags.LOCATION_TAG, attribute_name);
        parent_element.appendChild(child_element);

        return child_element;
    }

    public XMLElement addElement(XMLElement parent_element,
                                 String element_name, String attrib_name,
                                 String attrib_data) {
        XMLElement child_element =
            (XMLElement)xml_document.createElement(element_name);
        if (attrib_name != null) {
            child_element.setAttribute(attrib_name, attrib_data);
        }
        parent_element.appendChild(child_element);

        return child_element;
    }
}
